home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / RCS / div.c,v < prev    next >
Encoding:
Text File  |  1988-05-21  |  1.6 KB  |  84 lines

  1. head     1.1;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.1
  9. date     88.05.21.12.14.42;  author ouster;  state Exp;
  10. branches ;
  11. next     ;
  12.  
  13.  
  14. desc
  15. @@
  16.  
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @/* 
  25.  * div.c --
  26.  *
  27.  *    Contains the source code for the "div" library procedure.
  28.  *
  29.  * Copyright 1988 Regents of the University of California
  30.  * Permission to use, copy, modify, and distribute this
  31.  * software and its documentation for any purpose and without
  32.  * fee is hereby granted, provided that the above copyright
  33.  * notice appear in all copies.  The University of California
  34.  * makes no representations about the suitability of this
  35.  * software for any purpose.  It is provided "as is" without
  36.  * express or implied warranty.
  37.  */
  38.  
  39. #ifndef lint
  40. static char rcsid[] = "$Header: proto.c,v 1.2 88/03/11 08:39:08 ouster Exp $ SPRITE (Berkeley)";
  41. #endif not lint
  42.  
  43. #include "stdlib.h"
  44.  
  45. /*
  46.  *----------------------------------------------------------------------
  47.  *
  48.  * div --
  49.  *
  50.  *    Compute the quotient and remainder of the division of numer
  51.  *    by denom.
  52.  *
  53.  * Results:
  54.  *    The return value is j, unless j is negative, in which case
  55.  *    the return value is -j.
  56.  *
  57.  * Side effects:
  58.  *    None.
  59.  *
  60.  *----------------------------------------------------------------------
  61.  */
  62.  
  63. div_t
  64. div(numer, denom)
  65.     int numer;            /* Number to divide into. */
  66.     int denom;            /* Number that's divided into it. */
  67. {
  68.     div_t result;
  69.  
  70.     result.quot = numer/denom;
  71.     result.rem = numer%denom;
  72.     if ((result.rem ^ numer) < 0) {
  73.     if (result.rem < 0) {
  74.         result.rem += denom;
  75.         result.quot -= 1;
  76.     } else {
  77.         result.rem -= denom;
  78.         result.quot += 1;
  79.     }
  80.    }
  81.    return result;
  82. }
  83. @
  84.